package the.workshop.milktea.interfaces.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.wisteria.framework.util.DateUtils;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.dto.SelectedDTO;
import the.broccoli.auth.UserContext;
import the.workshop.milktea.infrastructure.model.IngredientCollect;
import the.workshop.milktea.interfaces.req.*;
import the.workshop.milktea.interfaces.resp.*;
import the.workshop.milktea.service.FirmwareService;
import the.workshop.milktea.service.IngredientCollectService;
import the.workshop.milktea.service.IngredientService;
import the.workshop.milktea.service.dto.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 奶茶原料 前端控制器
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Api(tags = "Tea")
@RestController
@RequestMapping("/milktea/ingredient")
public class IngredientController {

    @Resource
    private IngredientService ingredientService;

    @Resource
    private IngredientCollectService ingredientCollectService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody IngredientAddReq req) {
        IngredientAddDTO dto = new IngredientAddDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(ingredientService.add(dto));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@RequestBody IngredientModifyReq req) {
        IngredientModifyDTO dto = new IngredientModifyDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(ingredientService.modify(dto));
    }

    @PostMapping("/remove")
    public APIResult<Boolean> remove(@RequestBody IngredientRemoveReq req) {
        ingredientService.removeByIds(req.getIds(), UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/page")
    public APIResult<QueryResult<IngredientPageResp>> page(IngredientPageReq req) {
        IngredientPageDTO dto  = new IngredientPageDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setPageNum(req.getPageNum());
        dto.setPageSize(req.getPageSize());
        QueryResult queryResult = ingredientService.pageList(dto, req.getPageNum(), req.getPageSize());
        return APIResult.success(queryResult);
    }

    @GetMapping("/{id}")
    public APIResult<IngredientGetResp> get(@PathVariable(value = "id") Integer id) {
        IngredientGetResp resp = null;
        IngredientGetDTO dto = ingredientService.get(id);
        if (dto != null) {
            resp = new IngredientGetResp();
            BeanUtils.copyProperties(dto, resp);
        }
        return APIResult.success(resp);
    }

    @GetMapping("/list")
    public APIResult<List<IngredientPageResp>> getList() {
        return APIResult.success(ingredientService.getList());
    }

    @GetMapping("/selected/option")
    public APIResult<List<SelectedDTO>> getSelectedOption() {
        List<IngredientPageResp> list = ingredientService.getList();
        return APIResult.success(CollectionUtils.isEmpty(list) ? null : list.stream().map(this::convertOf).collect(Collectors.toList()));
    }

    @GetMapping("/collect/report")
    public APIResult<QueryResult<IngredientCollectReportResp>> report() {
        List<IngredientCollectReportResp> dataList = ingredientCollectService.report();
        return APIResult.success(new QueryResult<>(dataList.size(), dataList));
    }

    private SelectedDTO convertOf(IngredientPageResp source) {
        SelectedDTO target = new SelectedDTO();
        target.setDictCode(source.getIngredientId() + "");
        target.setDictName(source.getName());
        return target;
    }
}
